home *** CD-ROM | disk | FTP | other *** search
/ Disc to the Future 2 / Disc to the Future Part II Programmer's Reference (Wayzata Technology)(6013)(1992).bin / MAC / MACSHELL / MS1 / COMMANDS / CAT.C < prev    next >
Text File  |  1992-12-02  |  6KB  |  220 lines

  1. /*
  2.  *    MacShell Source File
  3.  *
  4.  *    Copyright (c) 1989, 1990, 1991, 1992  Suick Bay Technologies.  All rights reserved.
  5.  *
  6.  *
  7.  *    RESTRICTIONS ON MacShell program and source code.
  8.  *
  9.  *    Ñ╩MacShell¬ is a product of Suick Bay Technologies and is provided for
  10.  *    restricted use by the owner of the CDROM "Disk to the future II".
  11.  *
  12.  *    Ñ╩No permission is granted for any commercial use without the written
  13.  *    consent of the Suick Bay Technologies.
  14.  *
  15.  *    Ñ╩No permission is granted for any redistribution of any kind use without
  16.  *    the written consent of the Suick Bay Technologies.
  17.  *
  18.  *    Ñ╩Permission is granted to use this for any personal noncommercial use.
  19.  *
  20.  *    Ñ╩You may not distribute source or executable code at all, nor may you 
  21.  *    distribute it with or within a commercial product without the written
  22.  *    consent of the Suick Bay Technologies.  Please send modifications to 
  23.  *    the author for inclusion in updates to the program.  Thanks.
  24.  *
  25.  *
  26.  *    MacShell¬ IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
  27.  *    WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
  28.  *    PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
  29.  *
  30.  *    SUICK BAY TECHNOLOGIES SHALL HAVE NO LIABILITY WITH RESPECT TO THE
  31.  *    INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY MACSHELL
  32.  *    OR ANY PART THEREOF. 
  33.  *
  34.  *    In no event will Suick Bay Technologies be liable for any lost revenue
  35.  *    or profits or other special, indirect and consequential damages, even if
  36.  *    Suick Bay Technologies has been advised of the possibility of such damages.
  37.  *
  38.  *    Suick Bay Technologies can be reached at:
  39.  *    
  40.  *    8768 Cottonwood lane
  41.  *    Maple Grove, MN 55369
  42.  *    Voice: (612) 425-7025
  43.  *    AppleLink: D5233
  44.  *    
  45.  *    No parts of this software may be reproduced or stored in a
  46.  *    retrieval system or transmitted in any form, or any means,
  47.  *    electronic, mechanical, photocopying, recording or otherwise,
  48.  *    without the prior written permission of Suick Bay Technologies.
  49.  *    
  50.  *    Spread the word and not the disk.
  51.  *    
  52.  *    SPK 012290    :    Initial
  53.  */
  54.  
  55. #include    "SystemPub.h"
  56. #include    "Proc.h"
  57. #include    "ShellPub.h"
  58. #include    "Path.h"
  59.  
  60. #define        BUFSIZE        256
  61.  
  62. /*******************************************************************
  63.  *
  64.  *    Function cat
  65.  *
  66.  *    PathName Callback function
  67.  *
  68.  *    usage cat [options] [names]    
  69.  *
  70.  *******************************************************************/
  71.  
  72.  
  73. void        CatCallBack( WHandle ShellWh, int16 ProcID,
  74.                 char *path, char *last,
  75.                 pathType what, int16 vRefNum, int32 dirID )
  76. {
  77. char        readBuf[ BUFSIZE ];
  78. char        str[ 256 ];
  79. int16        err, fileRefNum;
  80. FInfo        finder;
  81. int32        count = BUFSIZE - 1;
  82. int32        catLen;
  83.  
  84.     if( what == pathIsFile )
  85.         {
  86.         strcpy( str, last );
  87.         CtoPstr( str );
  88.     
  89.         err = HGetFInfo( vRefNum, dirID, str, &finder );
  90.         
  91.         if( !err && (finder.fdType == 'TEXT' ))
  92.             {
  93.             err = HOpen( vRefNum, dirID, str, fsRdPerm, &fileRefNum );
  94.  
  95.             if( err )
  96.                 procPrintf( ShellWh, ProcID, "cat : can't cat %s (%d)\n",
  97.                     last, err );
  98.             else
  99.                 {
  100.                 err = GetEOF( fileRefNum, &catLen );
  101.                 
  102.                 while( !err && !UserAbort() )
  103.                     {
  104.                     err = FSRead( fileRefNum, &count, readBuf );
  105.                 
  106.                     readBuf[ count ] = '\0';
  107.                     
  108.                     if( !err || err == eofErr )
  109.                         StdOut( ShellWh, ProcID, readBuf );
  110.                     }
  111.         
  112.                 if( fileRefNum )    /* if a file open, close it */
  113.                     FSClose( fileRefNum );
  114.                 }
  115.             }
  116.         else
  117.             procPrintf( ShellWh, ProcID, "cat : %s is not a TEXT file.\n", last );
  118.         }
  119. }
  120.  
  121. /*******************************************************************/
  122.  
  123. CatFile( WHandle ShellWh, int16 ProcID, char *argument )
  124. {
  125. ShellWindRec    **MyShell;
  126. int16            matches; 
  127.  
  128.     MyShell = (ShellWindRec **) (**ShellWh).thing;
  129.  
  130.     matches = ExpandPath( ShellWh, ProcID, argument, (ProcPtr) CatCallBack,
  131.             (**MyShell).pwdVRefNum, (**MyShell).pwdDirID );
  132.  
  133.     ResetShellPWD( ShellWh );
  134. }
  135.  
  136. /*******************************************************************/
  137.  
  138. Boolean        DoCAT( int16 ProcToken, WHandle ShellWh, int16 ProcID, char *string )
  139. {
  140. int16        i, argc;
  141. char        *cp, argument[ 256 ];
  142. ShellWindRec    **MyShell;
  143.  
  144.     MyShell = (ShellWindRec **) (**ShellWh).thing;
  145.  
  146.     switch( ProcToken )
  147.         {
  148.         case    PROC_INIT    :
  149.             (**MyShell).Proc[ ProcID ].flags = TRUE;
  150.             (**MyShell).Proc[ ProcID ].options = 0L;
  151.             break;
  152.             
  153.         case    PROC_TERM    :
  154.         case    PROC_BREAK    :
  155.             /* Tell the shell that we're done */
  156.             SendOutToken( ShellWh, ProcID, PROC_BREAK );
  157.         
  158.             /* Turn ourself off */
  159.             (**MyShell).Proc[ ProcID ].ProcActive = FALSE;
  160.             break;
  161.             
  162.         case    PROC_STDIN    :
  163.             if( (**MyShell).Proc[ ProcID ].flags )
  164.                 {
  165.                 (**MyShell).Proc[ ProcID ].flags = FALSE;        
  166.  
  167.                 argc = (**MyShell).Proc[ ProcID ].argc;
  168.                 
  169.                 if( argc == 1 )
  170.                     (**MyShell).Proc[ ProcID ].options = 1L;
  171.                 else
  172.                     {
  173.                     for( i = 1; i < argc; i++ )
  174.                         {
  175.                         GetArgv( ShellWh, ProcID, i, argument );
  176.                         cp = argument;
  177.             
  178.                         if( *cp++ == '-' )
  179.                             {
  180.                             if( *cp == '\0' )    /* read from stdin */
  181.                                 (**MyShell).Proc[ ProcID ].options = 1L;
  182.                             else
  183.                                 while( *cp )
  184.                                     switch( *cp++ )
  185.                                         {
  186.                                         case    'u'    :    /* force */
  187.                                             break;
  188.                                         }
  189.                             }
  190.                         }
  191.                     for( i = 1; i < argc; i++ )
  192.                         {
  193.                         GetArgv( ShellWh, ProcID, i, argument );
  194.                         CatFile( ShellWh, ProcID, argument );
  195.                         }
  196.                     }
  197.  
  198.                 if( !(**MyShell).Proc[ ProcID ].options )
  199.                     {
  200.                     /* Tell the shell that we're done */
  201.                     SendOutToken( ShellWh, ProcID, PROC_BREAK );
  202.                 
  203.                     /* Turn ourself off */
  204.                     (**MyShell).Proc[ ProcID ].ProcActive = FALSE;
  205.                     (**MyShell).Proc[ ProcID ].flags = FALSE;        
  206.                     (**MyShell).Proc[ ProcID ].options = 0L;
  207.                     }
  208.                 
  209.                 return( FALSE );
  210.                 }
  211.  
  212.             if( (**MyShell).Proc[ ProcID ].options )
  213.                 {
  214.                 /* send stdin */
  215.                  StdOut( ShellWh, ProcID, string );
  216.                  break;
  217.                 }
  218.         }
  219. }
  220.